package com.ylbh.modules.statistics.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.statistics.UserListExcel;
import com.ylbh.common.pojo.statistics.dto.OperationStatisticsDTO;
import com.ylbh.common.pojo.statistics.vo.MembershipVO;
import com.ylbh.common.pojo.statistics.vo.UserListVO;
import com.ylbh.common.utils.ExcelUtils;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.modules.business.entity.ShoppingUser;
import com.ylbh.modules.statistics.mapper.OperationStatisticsMapper;
import com.ylbh.modules.statistics.service.OperationStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 运营统计
 * @author czx
 */
@Service
public class OperationStatisticsServiceImpl extends ServiceImpl<OperationStatisticsMapper, ShoppingUser> implements OperationStatisticsService {

    @Autowired
    private OperationStatisticsMapper operationStatisticsMapper;

    /**
     * 所有类型用户数据统计
     */
    public List<Integer> queryUserType(OperationStatisticsDTO operationStatisticsDTO){
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 1; i < 9; i++) {
            operationStatisticsDTO.setUserType(i);
            int count = operationStatisticsMapper.selectUserCount(operationStatisticsDTO);
            list.add(count);
        }
        return list;
    }

    /**
     * 类型下的所有用户
     * @param operationStatisticsDTO
     * @return
     */
    @Override
    public PageUtils queryUser(OperationStatisticsDTO operationStatisticsDTO) {
        Page<Object> page = new Page<>(operationStatisticsDTO.getPage(), operationStatisticsDTO.getLimit());
        operationStatisticsDTO.setUserType(operationStatisticsDTO.getUserType() + 1);
        List<UserListVO> user = operationStatisticsMapper.selectUser(operationStatisticsDTO, page);
        Page<UserListVO> page1 = new Page<>();
        return new PageUtils(page1.setTotal(page.getTotal()).setRecords(user));
    }

    /**
     * 运营统计
     * @param operationStatisticsDTO
     * @return
     */
    @Override
    public List<MembershipVO> queryMembership(OperationStatisticsDTO operationStatisticsDTO) {
        return operationStatisticsMapper.selectMembership(operationStatisticsDTO);
    }

    /**
     * 运营统计下的各类型下的下级
     * @param operationStatisticsDTO
     * @return
     */
    @Override
    public PageUtils queryMembershipLower(OperationStatisticsDTO operationStatisticsDTO) {
        Page<Object> page = new Page<>(operationStatisticsDTO.getPage(), operationStatisticsDTO.getLimit());
        List<UserListVO> user = operationStatisticsMapper.selectMembershipLower(operationStatisticsDTO, page);
        Page<UserListVO> page1 = new Page<>();
        return new PageUtils(page1.setTotal(page.getTotal()).setRecords(user));
    }

    /**
     * 运营统计导出
     * @param request
     * @param response
     * @throws NormalException
     */
    @Override
    public void exportMembershipLowerExcel(HttpServletRequest request, HttpServletResponse response) throws NormalException {
        String keywords = request.getParameter("keywords");
        List<UserListExcel> excel = operationStatisticsMapper.exportMembershipLowerExcel(keywords);
        ExcelUtils.exportExcel(excel, "运营统计", "", UserListExcel.class, System.currentTimeMillis() + ".xls", response);
    }

}
